package pkg.action;

import java.io.IOException;
import java.util.List;
import net.sourceforge.stripes.action.After;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.ForwardResolution;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.controller.LifecycleStage;
import net.sourceforge.stripes.validation.ValidationErrors;
import pkg.dao.EntityDAO;
import pkg.dao.PagedList;
import pkg.entity.Prefix;
import pkg.filter.PrefixFilter;

public class PrefixActionBean extends BaseActionBean {

    private PagedList<Prefix> prefixes;
    private List<Prefix> prefixList;
    private List<Long> prefixIds;
    private Integer page;
    private String sort;
    private String dir;
    private String mode;
    private String filterText;
    private Prefix prefix;

    public PrefixActionBean() {
    }

    @DefaultHandler
    public Resolution view() {
        loadPrefixes();
        return new ForwardResolution("/PrefixList.jsp");
    }

    public void loadPrefixes() {
        EntityDAO sb;

        sb = new EntityDAO();
        //TODO
        PrefixFilter f = new PrefixFilter();
        filterText = (String) getSession().getAttribute("filterText");
        if (filterText != null) {
            f.setSimple(filterText);
        }
        prefixList = sb.findAllByFilter(f);
        prefixes = new PagedList<Prefix>();
        prefixes.setResults(sb.findAllByFilter(f));
        if (page == null) {
            page = 1;
        }
        prefixes.setPageNumber(page);
    }

    public Resolution addNew() {
        return new RedirectResolution("/Prefix.action?create");
    }

    public Resolution deleteSelected() {
        EntityDAO sb = new EntityDAO();

        if (getPrefixIds() != null) {
            sb.delete(Prefix.class, prefixIds);
        }
        loadPrefixes();
        RedirectResolution result;
        result = new RedirectResolution(this.getClass());
        result.addParameter("page", page);
        return result;
    }

    public Resolution preEdit() {
        EntityDAO sb;
        sb = new EntityDAO();
        prefix = sb.find(Prefix.class, prefix.getId());
        loadLookups();
        return new ForwardResolution("/PrefixEdit.jsp");
    }

    public Resolution save() throws IOException {
        EntityDAO sb = new EntityDAO();
        Prefix origItem = null;

        if (prefix.getId() != null) {
            origItem = sb.find(Prefix.class, prefix.getId());
        }

//            if (origItem == null) {
//                ValidationError error = new SimpleError("Item code is invalid.");
//                getContext().getValidationErrors().add("itemCode", error);
//                return getContext().getSourcePageResolution();
//            }

        // Link up parent to children
        sb.update(prefix);
        RedirectResolution result;
        result = new RedirectResolution(this.getClass());
        result.addParameter("page", page);
        return result;
    }

    @After(stages=LifecycleStage.BindingAndValidation)
    public void loadRequestIfErrors() {
        ValidationErrors ve = getContext().getValidationErrors();
        if (ve.size() > 0 && prefix != null && prefix.getId() != null) {
            EntityDAO sb = new EntityDAO();
            prefix = sb.find(Prefix.class, prefix.getId());
        }
        loadLookups();
    }

    public Resolution create() {
        prefix = new Prefix();
        mode = "add";
        loadLookups();
        return new ForwardResolution("/PrefixEdit.jsp");
    }

    public Resolution applyFilter() {
        if (filterText != null) {
            getSession().setAttribute("filterText", filterText);
        } else {
            getSession().removeAttribute("filterText");
        }
        return new RedirectResolution(this.getClass());
    }


    public void loadLookups() {
        EntityDAO sb = new EntityDAO();
        // TODO
    }

    //<editor-fold defaultstate="collapsed" desc=" Setters/Getters ">
    public Prefix getPrefix() {
        return prefix;
    }

    public void setPrefix(Prefix category) {
        this.prefix = category;
    }

    public Prefix getItem() {
        return prefix;
    }

    public void setItem(Prefix item) {
        this.prefix = item;
    }

    public PagedList<Prefix> getPrefixes() {
        return prefixes;
    }

    public void setPrefixes(PagedList<Prefix> items) {
        this.prefixes = items;
    }

    public List<Prefix> getPrefixList() {
        return prefixList;
    }

    public void setPrefixList(List<Prefix> itemList) {
        this.prefixList = itemList;
    }

    public List<Long> getPrefixIds() {
        return prefixIds;
    }

    public void setPrefixIds(List<Long> PrefixIds) {
        this.prefixIds = PrefixIds;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public String getDir() {
        return dir;
    }

    public void setDir(String dir) {
        this.dir = dir;
    }

    public String getMode() {
        return mode;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }

    public String getFilterText() {
        return filterText;
    }

    public void setFilterText(String filterText) {
        this.filterText = filterText;
    }
    //</editor-fold>

}
